<?php

class Completion extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'tbl_comment':
	 * @var integer $id
	 * @var string $content
	 * @var integer $status
	 * @var integer $created
	 * @var string $author
	 * @var string $email
	 * @var string $url
	 * @var integer $post_id
	 */
	const STATUS_PENDING=1;
	const STATUS_APPROVED=2;

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{completion}}';
	}

	/**
	 * @return array validation rules for model attributes.

	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			//array('content, author, email', 'required'),
			//array('author, email, url', 'length', 'max'=>128),
			//array('email','email'),
			//array('url','url'),
		);
	}
	 */
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)

	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'content' => 'Comment',
			'status' => 'Status',
			'create_time' => 'Create Time',
			'author' => 'Name',
			'email' => 'Email',
			'url' => 'Website',
			'page' => 'Page',
		);
	}
	 */
        
        public function setAnswer($question)
        {
            
            $model=Question::model()->findByPk($question->theId);
            //$this=  Completion::model()->findBySql('Select * from tbl_completion where exam='.$question->theExam.' and question='.$question->theId);
            $result=0;
            if($model->answer == $question->theAnswer) {
                $result=1;
                $this->passed = $this->passed + 1;
                //unset($actual[$id]);
                //Yii::app()->session['questions']=$actual;
            }
            else $this->failed = $this->failed + 1;
            $this->question=$question->theId;
            $this->exam=$question->theExam;
            $this->examdate=new CDbExpression('NOW()');
            $this->result=$result;
            
            $this->save();

        }
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function getQuestion($exam, $examtype, $questionnaire)
	{
                $result='Select id from tbl_question where questionnaire='.$questionnaire;
                if($examtype >1 ) $result=$result.' and id not in 
                            (Select question from tbl_completion where exam = '.$exam.' and result = 1)';
                //$all = Question::model()->findByAttributes ('id', 'not in (Select question from tbl_completion)');
		$all=Yii::app()->db->createCommand($result)->queryAll();
                $actual=array();
                foreach($all as $q){
                    array_push($actual,$q['id']);
                }
		$total=Yii::app()->db->createCommand(
                        'Select count(*) as cnt from tbl_question where questionnaire='.$questionnaire)->queryRow();
            if(sizeof($actual) ==0)  
                            return array('model'=>new Question,'stats'=>array('total'=>$total['cnt'],
                                                        'rest'=>sizeof($actual),
                ));
            $model=Question::model()->findByPk( $actual[rand(0,sizeof($actual)-1)] );   
            return array('model'=>$model,'stats'=>array('total'=>$total['cnt'],
                                                        'rest'=>sizeof($actual),
                ));
	}


}